package com.example.administrator.dagger2demo.http;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Authenticator;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016/4/15.
 */
public class Retrofit2okhttp3 {

    public Retrofit2okhttp3() {
        Interceptor mTokenInterceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request originalRequest = chain.request();
                if ("YOUR.sToken" == null || alreadyHasAuthorizationHeader(originalRequest)) {
                    return chain.proceed(originalRequest);
                }
                Request authorised = originalRequest.newBuilder()
                        .header("Authorization", "Your.sToken")
                        .build();
                return chain.proceed(authorised);
            }
        };

        Authenticator mAuthenticator = new Authenticator() {
            @Override public Request authenticate(Route route, Response response)
                    throws IOException {
//                Your.sToken = service.refreshToken();
                return response.request().newBuilder()
                        .addHeader("Authorization"," Your.sToken")
                        .build();
            }
        };
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .authenticator(mAuthenticator)
                .retryOnConnectionFailure(true)
                .connectTimeout(15, TimeUnit.SECONDS)
                .addNetworkInterceptor(mTokenInterceptor)
                .build();
   /*   解释：
        HttpLoggingInterceptor 是一个拦截器，用于输出网络请求和结果的 Log，
        可以配置 level 为 BASIC / HEADERS / BODY，都很好理解，
        对应的是原来 retrofit 的 set log level 方法，现在 retrofit 已经没有这个方法了，
        所以只能到 OkHttp 这边来配置，并且 BODY 对应原来到 FULL.
        retryOnConnectionFailure 方法为设置出现错误进行重新连接。
        connectTimeout 设置超时时间
        addNetworkInterceptor 让所有网络请求都附上你的拦截器，我这里设置了一个 token 拦截器，
        就是在所有网络请求的 header 加上 token 参数，下面会稍微讲一下这个内容。*/

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("BASE_URL")
                .client(okHttpClient)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RestApi  service = retrofit.create(RestApi.class);
    }


    /**
     * 认证
     *
     * @param originalRequest
     * @return
     */
    public boolean alreadyHasAuthorizationHeader(Request originalRequest) {
        return false;
    }
}
